#! /usr/bin/python3
#******************************************************************************
# Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
# licensed under the Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#     http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
# PURPOSE.
# See the Mulan PSL v2 for more details.
# ******************************************************************************/
"""
This is openEuler member counter
"""
import logging
import csv
import sys
import os
import argparse
import re
import codecs
from  src.api.gitee import GiteeAPI
from src.libs.logger import logger

class MemberCounter(object):
    def __init__(self):
        """
        """

    def get_openeuler_member(self, enterprise):
        """
        """
        gitee = GiteeAPI(version='v5', enterprise='openEuler')
        with codecs.open("openeuler_enterprise_members.csv", 'w', encoding='gbk') as result_file:
            writer = csv.writer(result_file)
            writer.writerow(["gitee_id", "remark/name"])
            for i in range(1,5):
                enterprise_members_dict = gitee.get_openeuler_mambers(enterprise, i)
                for member_line in enterprise_members_dict:
                    gitee_id = "NA"
                    gitee_name = member_line.get("remark")
                    
                    user = member_line.get("user")
                    if user:
                        gitee_id = user.get("login")
                    
                    if not gitee_name:
                        gitee_name = user.get("name")
                    logger.info(f"{gitee_id}:{gitee_name}")

                    writer.writerow([gitee_id, gitee_name])

def main():
    """
    Main entrance of the functionality
    """
    params = argparse.ArgumentParser()
    params.add_argument("-e", "--enterprise", type=str, required=True,
                        help="select the enterprise")

    args = params.parse_args()

    if not args.enterprise:
        print("args invalid, please use -h for help")
        quit()

    MemberCounter().get_openeuler_member(args.enterprise)

if __name__ == "__main__":
    main()